/**
 * 导航工具函数
 * 封装 uni 跳转方法，支持对象方式传参
 */

/**
 * 参数类型定义
 */
interface NavigateOptions {
  url: string
  params?: Record<string, any>
  [key: string]: any
}

/**
 * 将对象参数转换为 URL 查询字符串
 * @param params 参数对象
 * @returns 查询字符串
 */
function objectToQueryString(params: Record<string, any>): string {
  if (!params || Object.keys(params).length === 0) {
    return ''
  }

  const queryParams = Object.entries(params)
    .filter(([_, value]) => value !== undefined && value !== null)
    .map(([key, value]) => {
      // 对复杂类型进行 JSON 序列化，并进行 URL 编码
      const encodedValue = typeof value === 'object'
        ? encodeURIComponent(JSON.stringify(value))
        : encodeURIComponent(String(value))
      return `${encodeURIComponent(key)}=${encodedValue}`
    })
    .join('&')

  return queryParams ? `?${queryParams}` : ''
}

/**
 * 构建完整的导航 URL
 * @param options 导航选项
 * @returns 完整的 URL
 */
function buildNavigationUrl(options: NavigateOptions): string {
  const { url, params } = options
  const baseUrl = url.split('?')[0] // 移除可能已存在的查询参数
  const queryString = objectToQueryString(params || {})
  return `${baseUrl}${queryString}`
}

/**
 * 页面导航 - navigateTo
 * 保留当前页面，跳转到应用内的某个页面
 * @param options 导航选项
 * @returns Promise
 */
export function navigateTo(options: NavigateOptions): Promise<any> {
  const fullUrl = buildNavigationUrl(options)
  const { params, ...restOptions } = options

  return new Promise((resolve, reject) => {
    uni.navigateTo({
      ...restOptions,
      url: fullUrl,
      success: resolve,
      fail: reject,
    })
  })
}

/**
 * 页面导航 - redirectTo
 * 关闭当前页面，跳转到应用内的某个页面
 * @param options 导航选项
 * @returns Promise
 */
export function redirectTo(options: NavigateOptions): Promise<any> {
  const fullUrl = buildNavigationUrl(options)
  const { params, ...restOptions } = options

  return new Promise((resolve, reject) => {
    uni.redirectTo({
      ...restOptions,
      url: fullUrl,
      success: resolve,
      fail: reject,
    })
  })
}

/**
 * 页面导航 - reLaunch
 * 关闭所有页面，打开到应用内的某个页面
 * @param options 导航选项
 * @returns Promise
 */
export function reLaunch(options: NavigateOptions): Promise<any> {
  const fullUrl = buildNavigationUrl(options)
  const { params, ...restOptions } = options

  return new Promise((resolve, reject) => {
    uni.reLaunch({
      ...restOptions,
      url: fullUrl,
      success: resolve,
      fail: reject,
    })
  })
}

/**
 * 页面导航 - switchTab
 * 跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面
 * @param options 导航选项
 * @returns Promise
 */
export function switchTab(options: NavigateOptions): Promise<any> {
  const fullUrl = buildNavigationUrl(options)
  const { params, ...restOptions } = options

  return new Promise((resolve, reject) => {
    uni.switchTab({
      ...restOptions,
      url: fullUrl,
      success: resolve,
      fail: reject,
    })
  })
}

/**
 * 页面导航 - navigateBack
 * 关闭当前页面，返回上一页面或多级页面
 * @param delta 返回的页面数，如果 delta 大于现有页面数，则返回到首页
 * @returns Promise
 */
export function navigateBack(delta: number = 1): Promise<any> {
  return new Promise((resolve, reject) => {
    uni.navigateBack({
      delta,
      success: resolve,
      fail: reject,
    })
  })
}

/**
 * 导航工具集合
 */
export const uniNavigation = {
  navigateTo,
  redirectTo,
  reLaunch,
  switchTab,
  navigateBack,
}

export default uniNavigation
